﻿namespace Manhua2.Helper
{
    using HtmlAgilityPack;
    using System.Collections.Generic;

    public static class HtmlHelper
    {
        public static List<HtmlNode> GetElementByClass(this HtmlNode body, string name)
        {
            List<HtmlNode> results = new List<HtmlNode>();
            foreach (HtmlNode s in body.ChildNodes)
            {
                if (s.GetAttributeValue("class", string.Empty) == name)
                {
                    results.Add(s);
                    continue;
                }

                if (s.ChildNodes.Count != 0)
                {
                    var nodes = GetElementByClass(s, name);
                    results.AddRange(nodes);
                }
            }

            return results;
        }

        public static List<HtmlNode> GetElements(this HtmlNode body, string name)
        {
            List<HtmlNode> results = new List<HtmlNode>();
            foreach (HtmlNode s in body.ChildNodes)
            {
                if (s.Name == name)
                {
                    results.Add(s);
                    continue;
                }

                if (s.ChildNodes.Count != 0)
                {
                    var nodes = GetElements(s, name);
                    results.AddRange(nodes);
                }
            }

            return results;
        }

        public static HtmlNode GetElement(this HtmlNode body, string name)
        {
            foreach (HtmlNode s in body.ChildNodes)
            {
                if (s.Name == name)
                {
                    return s;
                }

                if (s.ChildNodes.Count != 0)
                {
                    return GetElement(s, name);
                }
            }

            return null;
        }
    }
}
